//
//  UITextField+Additions.swift
//  live
//
//  Created by leo on 16/7/13.
//  Copyright © 2016年 io.ltebean. All rights reserved.
//

import UIKit

extension UITextField {
    
    private struct AssociatedKeys {
        static var insetBy = "UITextFiled_Additions_insetBy_associatedKey"
    }
    
    @IBInspectable var insetBy: CGPoint {
        set {
            objc_setAssociatedObject(self, &AssociatedKeys.insetBy, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
            setNeedsLayout()
        }
        get { return objc_getAssociatedObject(self, &AssociatedKeys.insetBy) as? CGPoint ?? .zero }
    }
    
    @objc func _textRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: insetBy.x , dy: insetBy.y)
    }
    
    @objc func _editingRect(forBounds bounds: CGRect) -> CGRect {
        return bounds.insetBy(dx: insetBy.x , dy: insetBy.y)
    }
}

extension UITextField: MethodSwizzling {
    
    static func swizzling() {
        swizzle(self, selector: #selector(textRect(forBounds:)), with: #selector(_textRect(forBounds:)))
        swizzle(self, selector: #selector(editingRect(forBounds:)), with: #selector(_editingRect(forBounds:)))
    }
}


